// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) braces deadcode 

package net.minecraft.src;

import java.io.*;
import net.minecraft.client.Minecraft;
// Referenced classes of package net.minecraft.src:
//            TileEntity, IInventory, ItemStack, NBTTagCompound, 
//            NBTTagList, World, Item, BlockFurnace, 
//            FurnaceRecipes, Block, Material, ModLoader, 
//            EntityPlayer

public class TileEntityNetherrackFurnace extends TileEntity
    implements IInventory
{

    public TileEntityNetherrackFurnace()
    {
        furnaceItemStacks = new ItemStack[3];
        furnaceBurnTime = 0;
        currentItemBurnTime = 0;
        furnaceCookTime = 0;
        //debug log
        /*try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());
            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("--TileEntity-started");
            pw.close();
        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
    }

    public int getSizeInventory()
    {
                //debug log
        /*try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());
            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("TileEntity-getSizeInventory");
            pw.close();
        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
        return furnaceItemStacks.length;
    }

    public ItemStack getStackInSlot(int i)
    {
                //debug log
        /*try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());
            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("TileEntity-getStackInSlot");
            fw.close();
        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
        return furnaceItemStacks[i];
    }

    public ItemStack decrStackSize(int i, int j)
    {
        //debug log
        /*try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());
            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("TileEntity-decrStackSize");
            pw.close();
        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
        if(furnaceItemStacks[i] != null)
        {
            if(furnaceItemStacks[i].stackSize <= j)
            {
                ItemStack itemstack = furnaceItemStacks[i];
                furnaceItemStacks[i] = null;
                return itemstack;
            }
            ItemStack itemstack1 = furnaceItemStacks[i].splitStack(j);
            if(furnaceItemStacks[i].stackSize == 0)
            {
                furnaceItemStacks[i] = null;
            }
            return itemstack1;
        } else
        {
            return null;
        }
    }

    public void setInventorySlotContents(int i, ItemStack itemstack)
    {
        furnaceItemStacks[i] = itemstack;
        if(itemstack != null && itemstack.stackSize > getInventoryStackLimit())
        {
            itemstack.stackSize = getInventoryStackLimit();
        }
                //debug log
        /*try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());
            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("TileEntity-setInventorySlotContents");
            pw.close();
        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
    }

    public String getInvName()
    {
                //debug log
        /*try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());
            ////log.createNewFile();
            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("TileEntity-getInvName");
            pw.close();
        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
        return "Netherrack Furnace";
    }

    public void readFromNBT(NBTTagCompound nbttagcompound)
    {
        super.readFromNBT(nbttagcompound);
        NBTTagList nbttaglist = nbttagcompound.getTagList("Items");
        furnaceItemStacks = new ItemStack[getSizeInventory()];
        for(int i = 0; i < nbttaglist.tagCount(); i++)
        {
            NBTTagCompound nbttagcompound1 = (NBTTagCompound)nbttaglist.tagAt(i);
            byte byte0 = nbttagcompound1.getByte("Slot");
            if(byte0 >= 0 && byte0 < furnaceItemStacks.length)
            {
                furnaceItemStacks[byte0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
            }
        }

        furnaceBurnTime = nbttagcompound.getShort("BurnTime");
        furnaceCookTime = nbttagcompound.getShort("CookTime");
        currentItemBurnTime = getItemBurnTime(furnaceItemStacks[1]);
                //debug log
        /*try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());
            //log.createNewFile();
            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("TileEntity-readFromNBT");
            pw.close();
        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
    }

    public void writeToNBT(NBTTagCompound nbttagcompound)
    {
        super.writeToNBT(nbttagcompound);
        nbttagcompound.setShort("BurnTime", (short)furnaceBurnTime);
        nbttagcompound.setShort("CookTime", (short)furnaceCookTime);
        NBTTagList nbttaglist = new NBTTagList();
        for(int i = 0; i < furnaceItemStacks.length; i++)
        {
            if(furnaceItemStacks[i] != null)
            {
                NBTTagCompound nbttagcompound1 = new NBTTagCompound();
                nbttagcompound1.setByte("Slot", (byte)i);
                furnaceItemStacks[i].writeToNBT(nbttagcompound1);
                nbttaglist.setTag(nbttagcompound1);
            }
        }

        nbttagcompound.setTag("Items", nbttaglist);
                //debug log
        /*try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());
            //log.createNewFile();
            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("TileEntity-writeToNBT");
            pw.close();
        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
    }

    public int getInventoryStackLimit()
    {
                //debug log
        /*try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());
            //log.createNewFile();
            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("TileEntity-getInventoryStackLimit");
            pw.close();
        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
        return 64;
    }

    public int getCookProgressScaled(int i)
    {
                //debug log
        /*try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());
            //log.createNewFile();
            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("TileEntity-getCookProgressScaled");
            pw.close();
        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
        return (furnaceCookTime * i) / 200;
    }

    public int getBurnTimeRemainingScaled(int i)
    {
                //debug log
        /*try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());
            //log.createNewFile();
            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("TileEntity-getBurnTimeRemainingScaled");
            pw.close();
        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
        if(currentItemBurnTime == 0)
        {
            currentItemBurnTime = 200;
        }
        return (furnaceBurnTime * i) / currentItemBurnTime;
    }

    public boolean isBurning()
    {
                //debug log
        /*try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());
            //log.createNewFile();
            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("TileEntity-isBurning");
            pw.close();
        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
        return furnaceBurnTime > 0;
    }

    public void updateEntity()
    {
                //debug log
        /*try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());
            //log.createNewFile();
            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("TileEntity-updateEntity");
            pw.close();
        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
        boolean flag = furnaceBurnTime > 0;
        boolean flag1 = false;
        if(furnaceBurnTime > 0)
        {
            furnaceBurnTime--;
        }
        if(!worldObj.multiplayerWorld)
        {
            if(furnaceBurnTime == 0 && canSmelt())
            {
                currentItemBurnTime = furnaceBurnTime = getItemBurnTime(furnaceItemStacks[1]);
                if(furnaceBurnTime > 0)
                {
                    flag1 = true;
                    if(furnaceItemStacks[1] != null)
                    {
                        if(furnaceItemStacks[1].getItem().hasContainerItem())
                        {
                            furnaceItemStacks[1] = new ItemStack(furnaceItemStacks[1].getItem().getContainerItem());
                        } else
                        {
                            furnaceItemStacks[1].stackSize--;
                        }
                        if(furnaceItemStacks[1].stackSize == 0)
                        {
                            furnaceItemStacks[1] = null;
                        }
                    }
                }
            }
            if(isBurning() && canSmelt())
            {
                furnaceCookTime++;
                if(furnaceCookTime == 200)
                {
                    furnaceCookTime = 0;
                    smeltItem();
                    flag1 = true;
                }
            } else
            {
                furnaceCookTime = 0;
            }
            if(flag != (furnaceBurnTime > 0))
            {
                flag1 = true;
                BlockNetherrackFurnace.updateFurnaceBlockState(furnaceBurnTime > 0, worldObj, xCoord, yCoord, zCoord);
            }
        }
        if(flag1)
        {
            onInventoryChanged();
        }
    }

    private boolean canSmelt()
    {
                //debug log
        /*try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());
            //log.createNewFile();
            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("TileEntity-canSmelt");
            pw.close();
        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
        if(furnaceItemStacks[0] == null)
        {
            return false;
        }
        ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(furnaceItemStacks[0].getItem().shiftedIndex);
        if(itemstack == null)
        {
            return false;
        }
        if(furnaceItemStacks[2] == null)
        {
            return true;
        }
        if(!furnaceItemStacks[2].isItemEqual(itemstack))
        {
            return false;
        }
        if(furnaceItemStacks[2].stackSize < getInventoryStackLimit() && furnaceItemStacks[2].stackSize < furnaceItemStacks[2].getMaxStackSize())
        {
            return true;
        }
        return furnaceItemStacks[2].stackSize < itemstack.getMaxStackSize();
    }

    public void smeltItem()
    {
        if(!canSmelt())
        {
            return;
        }
        ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(furnaceItemStacks[0].getItem().shiftedIndex);
        if(furnaceItemStacks[2] == null)
        {
            furnaceItemStacks[2] = itemstack.copy();
        } else
        if(furnaceItemStacks[2].itemID == itemstack.itemID)
        {
            furnaceItemStacks[2].stackSize += itemstack.stackSize;
        }
        if(furnaceItemStacks[0].getItem().hasContainerItem())
        {
            furnaceItemStacks[0] = new ItemStack(furnaceItemStacks[0].getItem().getContainerItem());
        } else
        {
            furnaceItemStacks[0].stackSize--;
        }
        if(furnaceItemStacks[0].stackSize <= 0)
        {
            furnaceItemStacks[0] = null;
        }
                //debug log
        /*try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());
            //log.createNewFile();
            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("TileEntity-smeltItem");
            pw.close();
        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
    }

    private int getItemBurnTime(ItemStack itemstack)
    {
                //debug log
        /*try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());
            //log.createNewFile();
            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("TileEntity-getItemBurnTime");
            pw.close();
        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
        if(itemstack == null)
        {
            return 0;
        }
        int i = itemstack.getItem().shiftedIndex;
        if(i < 256 && Block.blocksList[i].blockMaterial == Material.wood)
        {
            return 300*2;
        }
        if(i == Item.stick.shiftedIndex)
        {
            return 100*2;
        }
        if(i == Item.coal.shiftedIndex)
        {
            return 1600*2;
        }
        if(i == Item.bucketLava.shiftedIndex)
        {
            return 20000*2;
        }
        if(i == Block.sapling.blockID)
        {
            return 100*2;
        } else
        {
            return ModLoader.AddAllFuel(i, itemstack.getItemDamage())*2;
        }
    }

    public boolean canInteractWith(EntityPlayer entityplayer)
    {
                //debug log
        /*try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());
            //log.createNewFile();
            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("TileEntity-canInteractWith");
            pw.close();
        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
        if(worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) != this)
        {
            return false;
        }
        return entityplayer.getDistanceSq((double)xCoord + 0.5D, (double)yCoord + 0.5D, (double)zCoord + 0.5D) <= 64D;
    }
    
    public void openChest()
    {
    }

    public void closeChest()
    {
    }

    private ItemStack furnaceItemStacks[];
    public int furnaceBurnTime;
    public int currentItemBurnTime;
    public int furnaceCookTime;
}
